package my.mypool.core.thread;

import java.util.List;
import java.util.Vector;


import my.mypool.core.PoolablePool;
public class ThreadClient {
     private PoolablePool pool = null;
     public ThreadClient(int maxIdle, int initIdleCapacity) throws Exception {
          pool = new PoolablePool(PoolableThread.class,null,maxIdle,initIdleCapacity);
     }
     
     List<PoolableThread> listThreads = new Vector<PoolableThread>();
     
     public void destoryAllThread() throws Exception {
    	 /*
    	for(PoolableThread thread : listThreads) {
    		
    		pool.returnObject(thread);
     
		}
		*/
     }
     
     public void run(Taskable task) throws Exception{
          PoolableThread thread=pool.borrowObject();
          
          thread.runTask(new TaskWrapper(pool,thread,task));
          listThreads.add(thread);
     }

     public static void main(String[] args) throws Exception {
    	 ThreadClient client=new ThreadClient(5,10);
         for(int i=0;i<20;i++){
        	 client.run(new MyTask(""+i));
         }
     }
     
     static class MyTask implements Taskable{
         private String info="";
         public MyTask(String info){
             this.info=info;
         }

         public void doTask() {
            System.out.println(info);
         }
     }     
}
